package cn.javabb.framework.querys;

import cn.hutool.core.collection.CollUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2020/07/12 17:27
 */
public class MySqlQuery extends AbstractDbQuery{

    @Override
    public String tablesSql() {
        return "show table status WHERE 1=1 ";
    }

    @Override
    public String tablesSqlByNames(List<String> names) {
        List<String> formatNames = names.stream().map(t->"'"+t+"'").collect(Collectors.toList());
        return "show table status WHERE 1=1 and name in ("+CollUtil.join(formatNames,",")+")";
    }

    @Override
    public String tableFieldsSql() {
        return "show full fields from `%s`";
    }


    @Override
    public String tableName() {
        return "NAME";
    }


    @Override
    public String tableComment() {
        return "COMMENT";
    }


    @Override
    public String fieldName() {
        return "Field";
    }


    @Override
    public String fieldType() {
        return "Type";
    }


    @Override
    public String fieldComment() {
        return "Comment";
    }


    @Override
    public String fieldKey() {
        return "Key";
    }
    /**
     * 判断是否为自增主键
     * @param results
     * @return
     * @throws SQLException
     */
    @Override
    public boolean isKeyIdentity(ResultSet results) throws SQLException {
        return "auto_increment".equals(results.getString("extra"));
    }

    @Override
    public boolean isNotNull(ResultSet results) throws SQLException {
        return "NO".equals(results.getString("null"));
    }
}
